//
//  AskWhenOpeningController.m
//  WOVOpener
//
//  Created by James Tuley on 4/20/08.
//  Copyright 2008 James Tuley. All rights reserved.
//

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is WOV Suite.
 *
 * The Initial Developer of the Original Code is
 * James Tuley.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *           James Tuley <jay+wov@tuley.name> (WOV Suite Author)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#import "AskWhenOpeningController.h"


@implementation AskWhenOpeningController
-(NSWindow*)window{
	return window;
}

-(void)loadMenu{
	[fileTable reloadData];
	
	NSMenu* tMenu =[[NSMenu alloc] init];
	
	NSArray *anArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"KnownApps"];
	NSEnumerator *enumerator = [anArray objectEnumerator];
	NSDictionary* object;
	
	[tMenu addItemWithTitle:@"Pick an Application" action:NULL keyEquivalent:@""];
	[tMenu addItem:[NSMenuItem separatorItem]]; 
	NSMenuItem* tSelectedItem=nil;
	while ((object = [enumerator nextObject])) {
		NSString* tPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:[object objectForKey:@"Name"]];
		
	
		NSMenuItem* tItem =[[NSMenuItem alloc] initWithTitle:[object objectForKey:@"Name"]  action:@selector(menuPick:) keyEquivalent: @""];
		[tItem setTarget:self];
		if(tPath==nil)
			[tItem setEnabled:NO];
		else
			[tItem setImage:[[NSWorkspace sharedWorkspace] iconForFile:tPath]];
		[tItem setRepresentedObject:object];
		[tMenu addItem:tItem];
		if([[[NSUserDefaults standardUserDefaults] stringForKey:@"OpenApp"] isEqualToString:[object objectForKey:@"Id"]]){
			tSelectedItem =tItem;
			[isQuickTime setState:[[object objectForKey:@"QT"] boolValue] ? NSOnState: NSOffState];
		}
	}
	NSMenuItem* tItem =[tMenu addItemWithTitle:@"Other..." action:@selector(menuOtherPick:) keyEquivalent:@""];
	[tItem setTarget:self];
	[appMenu setMenu:tMenu];
	[appMenu selectItem:tSelectedItem];
}

-(void)setArray:(NSArray*)anArray{
	storedArray = [anArray mutableCopy];
}
-(NSArray*)array;
{
	return storedArray;
}

-(IBAction)buttonClick:(id)sender{
	[NSApp stopModalWithCode:[sender tag]];
}

-(IBAction)menuPick:(id)sender{
	NSMenuItem* tItem=[appMenu selectedItem];
	[[NSUserDefaults standardUserDefaults] setObject:[[tItem representedObject]objectForKey:@"Id"] forKey:@"OpenApp"];
	[[NSUserDefaults standardUserDefaults] setObject:[[tItem representedObject]objectForKey:@"QT"] forKey:@"UsesMoov"];
	[self loadMenu];
}
-(IBAction)check:(id)sender{
	NSMenuItem* tItem=[appMenu selectedItem];
	[[NSUserDefaults standardUserDefaults] setBool:[isQuickTime state] == NSOnState forKey:@"UsesMoov"];
	
	NSMutableArray *anArray = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"KnownApps"] mutableCopy] autorelease];
	
	for(unsigned i=0;i<[anArray count];i++){
		id object = [anArray objectAtIndex:i];
		if([[[tItem representedObject] objectForKey:@"Id"] isEqualTo:[object objectForKey:@"Id"]]){
			NSMutableDictionary* tMutDict = [[object mutableCopy]autorelease];
			[tMutDict setObject:[NSNumber numberWithBool:[isQuickTime state] == NSOnState] forKey:@"QT"];
			[anArray replaceObjectAtIndex:i withObject:tMutDict];
		}
	}
	
	[[NSUserDefaults standardUserDefaults]  setObject:anArray forKey:@"KnownApps"];

	[self loadMenu];
}

-(IBAction)menuOtherPick:(id)sender{
	
	NSOpenPanel* tPanel = [NSOpenPanel openPanel];
	[tPanel runModalForDirectory:@"/Applications" file:nil types:[NSArray arrayWithObject:@"app"]];
	NSArray* tFilenames =[tPanel filenames];
	
	NSEnumerator *enumerator = [tFilenames objectEnumerator];
	id object;
	
	NSMutableArray* tArray = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"KnownApps"] mutableCopy] autorelease];
	NSString* tId =@"";
	bool tQt= YES;
	while ((object = [enumerator nextObject])) {
		NSBundle* tBundle =[NSBundle bundleWithPath:object];
		
		NSDictionary* tDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[tBundle bundleIdentifier],@"Id",
							   [[object lastPathComponent] stringByDeletingPathExtension],@"Name",
							   [NSNumber numberWithBool:[isQuickTime state] == NSOnState], @"QT",nil];
		
		tId = [tBundle bundleIdentifier];
		tQt =[isQuickTime state] == NSOnState;
		[tArray addObject:tDict];
	}
	
	[[NSUserDefaults standardUserDefaults]  setObject:tArray forKey:@"KnownApps"];
	[[NSUserDefaults standardUserDefaults]  setObject:tId forKey:@"OpenApp"];
	[[NSUserDefaults standardUserDefaults]  setBool:tQt forKey:@"UsesMoov"];

	[self loadMenu];

}

-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
	return [storedArray count];
}

-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
	NSString* tString = [storedArray objectAtIndex:rowIndex];
	if([[aTableColumn identifier] isEqualTo:@"icon"]){
		return [[NSWorkspace sharedWorkspace] iconForFile:tString];
	}else{
		return [tString lastPathComponent];
	}
}


@end
